package com.airforcehospital;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.xubeihong.xubeihong.HttpTool;
import org.springframework.util.StringUtils;

public class AirForceService {

    //口腔 deptId=1318797 皮肤科 1318754

    boolean first = true;
    String deptId="1318754";
    String want_date="2024-12-12";
    //皮肤科门诊  党育平  栗洪师 邓天政 口腔修复专业
    String want_doctor_name="党育平";
    String want_doctor_id="";
    //皮肤科门诊
    String deptName="";
    String schId="";
    String accessSchId="";

    public static void main(String[] args) throws Exception {
        AirForceService service = new AirForceService();
        boolean res = service.call();
        System.out.println(res);
    }

    public Boolean call() throws Exception {

        if(StringUtils.isEmpty(want_doctor_id)){
            //Thread.sleep(1500);
            JSONObject doc_list = query_doctor_list();
            System.out.println("医生列表"+doc_list);
            JSONArray doc_arr = doc_list.getJSONArray("data");
            for(Object doc:doc_arr) {
                JSONObject docJson = (JSONObject) doc;
                if (docJson.getString("docName").equals(want_doctor_name) && docJson.getString("schDate").equals(want_date)) {
                    want_doctor_id = docJson.getString("docId");
                    deptName = docJson.getString("deptName");
                    break;
                }
            }
        }

        if(first && !StringUtils.isEmpty(want_doctor_id) && StringUtils.isEmpty(schId)){
            Thread.sleep(2000);
            first=false;
            JSONArray arr = query_doctor_detail(want_doctor_id);
            System.out.println("医生当天详情"+arr);
            for(Object docObj:arr){
                JSONObject doc_day = (JSONObject) docObj;
                if(doc_day.getString("api_name").endsWith("112")){
                    JSONArray doc_data = doc_day.getJSONArray("data");
                    if(doc_data.size()>0){
                        for(Object ele:doc_data){
                            JSONObject eleJson = (JSONObject) ele;
                            if(eleJson.getInteger("remainNo")>0){
                                schId = eleJson.getString("schId");
                                accessSchId = eleJson.getString("accessSchId");
                                break;
                            }
                        }
                    }
                }
            }
        }

        if(!first){
            load_schedul();
        }

        if(!StringUtils.isEmpty(schId)){
            Thread.sleep(300);
            JSONObject time_list = query_doctor_time_list();
            System.out.println("时间段列表"+time_list);
            if(time_list.getBoolean("result")){
                for(Object ele:time_list.getJSONArray("data")){
                    JSONObject hourData = (JSONObject) ele;
                    if(hourData.getInteger("timeRemainNo")>0 && hourData.getInteger("state")==1){
                        String sTime = hourData.getString("expectStime");
                        String eTime = hourData.getString("expectEtime");
                        JSONObject submit_res = submit_order(sTime,eTime);
                        return  submit_res.getBoolean("result");
                        //System.out.println("订单结果\n"+submit_res);
                    }
                }
            }
        }

        return false;
    }

    public JSONObject query_doctor_time_list() throws Exception {

        String url = "https://m.hsyuntai.com/med/hp/hospitals/100578/registration/loadSource";
        JSONObject data = new JSONObject();
        data.put("schId",schId);
        data.put("accessSchId",accessSchId);
        data.put("timeout",30000);
        data.put("isLoading","true");

        return (JSONObject)AirHttpTool.post(url,data);
    }

    public JSONObject query_doctor_list() throws Exception {

        String url="https://m.hsyuntai.com/med/hp/hospitals/100578/registration/schList";
        JSONObject data =new JSONObject();
        data.put("regType","0");
        data.put("hosDistId","236");
        data.put("deptId",deptId);
        data.put("date","");
        data.put("timeout",30000);
        data.put("isLoading","true");

        JSONObject res = (JSONObject)AirHttpTool.post(url,data);

        return res;
    }

    public void load_schedul () throws Exception {

        Thread.sleep(500);
        String url = "https://m.hsyuntai.com/med/hp/hospitals/100578/registration/loadSchedulingTable225?accessNewSch=1&branchId=236&date="+want_date+"&doctorId="+want_doctor_id+"&filtrate=N&isShowTime=false&outpatientId="+deptId+"&type=0";

        JSONObject res = (JSONObject)AirHttpTool.get(url);

        if(res.getBoolean("result") && res.getJSONArray("data").size()>0){
            JSONObject time = (JSONObject)res.getJSONArray("data").get(0);
            if(time.getInteger("state")==1){
                schId = time.getString("schId");
                accessSchId = time.getString("accessSchId");
            }
        }

    }

    public JSONArray query_doctor_detail(String doctor_id) throws Exception {

        String url="https://m.hsyuntai.com/med/hp/hospitals/100578/registration/doctorDetails225?branchId=236&date="+want_date+"&docId="+doctor_id+"&filtrate=N&isShowTime=false&outpatientId="+deptId+"&schListType=true&type=0";

        return (JSONArray)AirHttpTool.get(url);
    }
    public JSONObject submit_order(String sTime,String eTime) throws Exception {

        Thread.sleep(200);
        String url = "https://m.hsyuntai.com/med/hp/hospitals/100578/registration/submit230";
        JSONObject data = new JSONObject();
        data.put("patId",33018182);
        data.put("patName","郝堙柳");
        data.put("pcId",29194412);
        data.put("schId",schId);
        data.put("isSendRegSms","Y");
        data.put("sTime",sTime);
        data.put("eTime",eTime);
        data.put("type","0");
        data.put("vcode","");
        data.put("docId",want_doctor_id);
        data.put("hosDistId","236");
        data.put("deptId",deptId);
        data.put("districtId","236");
        data.put("patCardType",6);
        data.put("patCardName","就诊ID号");
        data.put("deptName",deptName);
        data.put("isHealthCard","false");
        data.put("timeout",30000);
        data.put("isLoading","true");

        JSONObject inner = new JSONObject();
        inner.put("ChargeType","公费");

        data.put("regSelectItems",inner);

        JSONObject order_res = (JSONObject)AirHttpTool.post(url,data);
        System.out.println("订单结果"+order_res);
        //printJsonObject(order_res,"{");
        if(!order_res.getBoolean("result")){
            if(order_res.getString("msg").equals("触发gif图片验证码流程")){
                String bizToken = order_res.getJSONObject("data").getString("bizToken");
                JSONObject img_data = get_verify_img(bizToken);
                String img_code = img_data.getJSONObject("data").getString("imageCode");
                String verify = get_verify(img_code);
                System.out.println("验证码"+verify);

                JSONObject verify_res = check_verify(bizToken,verify);
                if(verify_res.getBoolean("result")){
                    return submit_order(sTime,eTime);
                }
            }else {
                Thread.sleep(200);
                return submit_order(sTime,eTime);
            }
        }
        return order_res;
    }

    public JSONObject check_verify(String biz_token,String verifyCode) throws Exception {
        Thread.sleep(200);
        String url = "https://m.hsyuntai.com/med/hp/hospitals/100578/common/checkImgVerify";
        JSONObject data = new JSONObject();
        data.put("bizToken",biz_token);
        data.put("verificationCode",verifyCode);
        data.put("timeout",30000);
        data.put("isLoading","true");

        JSONObject verify_res = (JSONObject)AirHttpTool.post(url,data);
        System.out.println("验证结果\n"+verify_res);
        return verify_res;

    }

    public String get_verify(String code) throws Exception {

        String url = "http://127.0.0.1:5000/image/recognize";
        JSONObject data = new JSONObject();
        data.put("code",code);
        String res = (String)AirHttpTool.post(url,data);
        return res;
    }

    public JSONObject get_verify_img(String biz_token) throws Exception {

        Thread.sleep(200);
        String url="https://m.hsyuntai.com/med/hp/hospitals/100578/common/refreshImgVerify";
        JSONObject data = new JSONObject();
        data.put("bizToken",biz_token);
        data.put("timeout",30000);
        data.put("isLoading","true");
        JSONObject img_res = (JSONObject)AirHttpTool.post(url,data);
        return img_res;
    }

    private void printJsonObject(JSONObject jsonObject, String prefix) {
        // 获取JSONObject的所有键
        for (String key : jsonObject.keySet()) {
            Object value = jsonObject.get(key);
            if (value instanceof JSONObject) {
                System.out.println(prefix + key + ": {");
                // 递归打印嵌套的JSONObject
                printJsonObject((JSONObject) value, prefix + "  ");
                System.out.println(prefix + "}");
            } else if (value instanceof JSONArray) {
                System.out.println(prefix + key + ": [");
                // 递归打印JSONArray中的元素
                //printJsonArray((JSONArray) value, prefix + "  ");
                System.out.println(prefix + "]");
            } else {
                System.out.println(prefix + key + ": " + value);
            }
        }
    }
}
